package com.social.socialservice.idal;

import com.social.socialservice.model.NotifyInfo;
import com.social.socialservice.unit.Pager;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 消息通知方法定义接口
 */

@Mapper
@Component
public interface INotifyDAL {
    /**
     * 查询消息分页
     * 通过当前用户id进行查询
     */
    @Select("SELECT ex_notify.*,ex_user.user_id,ex_user.username,ex_user.user_nickname,ex_user.user_headimg FROM ex_notify,ex_user\n" +
            "WHERE\n" +
            "ex_notify.opr_user_id=ex_user.user_id\n" +
            "AND\n" +
            "ex_notify.current_user_id=#{current_user_id}\n" +
            "ORDER BY ex_notify.notify_time DESC\n" +
            "LIMIT #{startIndex},#{pageSize};")
    public List<NotifyInfo> getNotifyMapperByCurrentUserId(Pager pager);

    /**
     * 查询消息分页的数据总数
     */
    @Select("SELECT count(*) FROM ex_notify,ex_user\n" +
            "WHERE\n" +
            "ex_notify.opr_user_id=ex_user.user_id\n" +
            "AND\n" +
            "ex_notify.current_user_id=#{current_user_id};")
    public Integer getNotifyMapperCountsByCurrentUserId(Pager pager);

    /**
     * 如果调用读取了查询消息的分页方法
     * 则修改未读消息为已读，即is_readed=1
     */
    @Update("update ex_notify set is_readed=1 where current_user_id=#{current_user_id}")
    public Integer updateSelfNotifyByCurrentUserId(Pager pager);

    /**
     * 消息提示中提及@用户的消息数据
     */
    @Select("SELECT ex_notify.*,ex_user.user_id,ex_user.username,ex_user.user_nickname,ex_user.user_headimg FROM ex_notify,ex_user\n" +
            "WHERE\n" +
            "ex_notify.opr_user_id=ex_user.user_id\n" +
            "AND\n" +
            "(ex_notify.is_quote=1\n" +
            "or\n" +
            "ex_notify.is_reply=1)\n" +
            "AND\n" +
            "ex_notify.current_user_id=#{current_user_id}\n" +
            "ORDER BY ex_notify.notify_time DESC\n" +
            "LIMIT #{startIndex},#{pageSize};")
    public List<NotifyInfo> getNotifyMapperATCurrentUserId(Pager pager);

    /**
     * 提及消息总数
     */
    @Select("SELECT count(*) FROM ex_notify,ex_user\n" +
            "WHERE\n" +
            "ex_notify.opr_user_id=ex_user.user_id\n" +
            "AND\n" +
            "(ex_notify.is_quote=1\n" +
            "or\n" +
            "ex_notify.is_reply=1)\n" +
            "AND\n" +
            "ex_notify.current_user_id=#{current_user_id};")
    public Integer getNotifyMapperCountsATCurrentUserId(Pager pager);

    /**
     * 互动消息指的是点赞和关注
     *
     */
    @Select("SELECT ex_notify.*,ex_user.user_id,ex_user.username,ex_user.user_nickname,ex_user.user_headimg FROM ex_notify,ex_user\n" +
            "WHERE\n" +
            "ex_notify.opr_user_id=ex_user.user_id\n" +
            "AND\n" +
            "(ex_notify.is_liked=1\n" +
            "or\n" +
            "ex_notify.is_foyou=1)\n" +
            "AND\n" +
            "ex_notify.current_user_id=#{current_user_id}\n" +
            "ORDER BY ex_notify.notify_time DESC\n" +
            "LIMIT #{startIndex},#{pageSize};")
    public List<NotifyInfo> getNotifyMapperHDCurrentUserId(Pager pager);

    /**
     * 互动消息指的是点赞和关注
     *  消息总数
     */
    @Select("SELECT count(*) FROM ex_notify,ex_user\n" +
            "WHERE\n" +
            "ex_notify.opr_user_id=ex_user.user_id\n" +
            "AND\n" +
            "(ex_notify.is_liked=1\n" +
            "or\n" +
            "ex_notify.is_foyou=1)\n" +
            "AND\n" +
            "ex_notify.current_user_id=#{current_user_id};")
    public Integer getNotifyMapperCountsHDCurrentUserId(Pager pager);

    /**
     * 添加消息通知
     * 1.判断当前回复、转发、点赞操作用户是自己不做操作
     * 2.如果消息已经存在过一条，则不操作，避免重复消息
     * 3.回复、点赞、转发、关注四个操作字段要进行非空判断，如果为空默认值为0
     */
    @Insert("<script>" +
            "insert into ex_notify(post_id,post_content," +
            "<if test='is_reply!=null and is_reply!=0'>" +
            "is_reply," +
            "</if>" +
            "<if test='is_quote!=null and is_quote!=0'>" +
            "is_quote," +
            "</if>" +
            "<if test='is_liked!=null and is_liked!=0'>" +
            "is_liked," +
            "</if>" +
            "<if test='is_foyou!=null and is_foyou!=0'>" +
            "is_foyou," +
            "</if>" +
            "opr_user_id,current_user_id)" +
            "values(#{post_id},#{post_content}," +
            "<if test='is_reply!=null and is_reply!=0'>" +
            "#{is_reply}," +
            "</if>" +
            "<if test='is_quote!=null and is_quote!=0'>" +
            "#{is_quote}," +
            "</if>" +
            "<if test='is_liked!=null and is_liked!=0'>" +
            "#{is_liked}," +
            "</if>" +
            "<if test='is_foyou!=null and is_foyou!=0'>" +
            "#{is_foyou}," +
            "</if>" +
            "#{opr_user_id},#{current_user_id})" +
            "</script>")
    public Integer addNotifyToUser(NotifyInfo notifyInfo);

    /**
     * 判断消息是否已经存在
     */
    @Select("select count(*) from ex_notify where opr_user_id=#{opr_user_id} and current_user_id=#{current_user_id} and post_id=#{post_id}")
    public Integer testNotifyIsExist(NotifyInfo notifyInfo);

    /**
     * 检索未读的通知消息数量
     */
    @Select("select count(*) from ex_notify where is_readed=0 and current_user_id=#{current_user_id}")
    public Integer getNotifyNotRead(NotifyInfo notifyInfo);
}
